function getName() {
    var user = JSON.parse(localStorage.getItem("loggedinUser"));

    if (user) {
        console.log("Currently logged in: " + user["name"]);
        document.getElementById("name-goes-here").innerText = user["name"];
        } else {
        console.log("No one is logged in");
    }
}
getName(); //run the function

function addCupListener(){
    document.getElementById("addCupButton").addEventListener("click",function(){
        //get the logged in user
        var user = JSON.parse(localStorage.getItem("loggedinUser"));
        if (user) {
            //increment the counter
            user["count"]++;
            // display it
            document.getElementById("coffee-count-goes-here").innerText = user["count"];
            //save into loggedinUser
            localStorage.setItem("loggedinUser", JSON.stringify(user));

            updateUserCount(user["email"],user["count"]);
        } else {
            console.log("No one is logged in");
        }
    })
}
addCupListener();

function updateUserCount(email,countValue){
    //get the users list
    var user = JSON.parse(localStorage.getItem("users"));

    if(user){
        const person = user.find(u => u.email === email);
        person.count =countValue;

        //save the date
        addToHistory(person);

        localStorage.setItem("users", JSON.stringify(user))
    }

}

function addToHistory(person){
    console.log("ADD HISTORY");
    //get the date and time 
    const now = new Date();
    const formatted = now.toLocaleString('en-US', {
        weekday: 'long', // "Monday", "Tuesday", etc.
        year: 'numeric', // "2021", "2022", etc.
        month: 'long', 
        // "January", "February", etc.
        day: 'numeric', 
        // 1, 2, 3, etc.
        hour: 'numeric', // 12 AM, 1 PM, etc.
        minute: 'numeric',
        second: 'numeric'
    });

    //add the date to history
    if (person.history === null) 
        //if there are no users yet
        person.history = []; 
    //initialize it to empty
    person.history.push({ date: formatted });
}

function getCount(){
    var user = JSON.parse(localStorage.getItem("loggedinUser")); 
    if (user) { 

        document.getElementById("coffee-count-goes-here").innerText = user["count"]; 
    } else { 
        console.log("No one is logged in"); 
    } 
}
getCount();

function addPlusMinusListener() { 
    document.addEventListener("DOMContentLoaded", function () { 
        const minusBtn = document.getElementById('minus-btn'); 
        const plusBtn = document.getElementById('plus-btn'); 
        const input = document.getElementById('number-input'); 

        minusBtn.addEventListener('click', function  () {
            if( input.value > 0){
                input.value = input.value - 1;
            }
        });

        plusBtn.addEventListener('click' , function (){
            input.value++;
        });
  
    }); 

}
addPlusMinusListener(); 
 
function addCupsListener() { 
    document.getElementById("addCupsButton").addEventListener("click", function () { 
        var number = document.getElementById('number-input').value;

        var user = JSON.parse(localStorage.getItem("loggedinUser"));
        if(user){
            user["count"] = Number (user["count"]) + Number(number);
            localStorage.setItem("loggedinUser", JSON.stringify(user)); 
            updateUserCount(user["email"], user["count"]); 
            document.getElementById("coffee-count-goes-here").innerText = user["count"]; 
        }else{
            console.log("No one is logged in"); 
        }
    });
     
} 
addCupsListener(); 